<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

// +----------------------------------------------------------------------
// | 余额管理
// +----------------------------------------------------------------------
namespace app\controller;
use think\facade\View;
use think\facade\Db;

class MendianMoney extends Common
{
    public function initialize(){
        parent::initialize();
        if(bid > 0) showmsg('无访问权限');
    }
    //余额明细
    public function moneylog(){
        if(request()->isAjax()){
            $page = input('param.page');
            $limit = input('param.limit');
            if(input('param.field') && input('param.order')){
                $order = 'mml.'.input('param.field').' '.input('param.order');
            }else{
                $order = 'mml.id desc';
            }

            $where = [];

            if($this->mdid){
                $where[] = ['mml.mdid','=',$this->mdid];
            }else{
                if(input('param.mdid')){
                    $where[] = ['mml.mdid','=',input('param.mdid')];
                }
            }
            $where[] = ['mml.aid','=',aid];
            if(input('param.name')) $where[] = ['mendian.name','like','%'.trim(input('param.name')).'%'];

            $count = 0 + Db::name('mendian_moneylog')
                ->alias('mml')
                ->join('mendian mendian','mendian.id=mml.mdid')
                ->where($where)
                ->count();
            $data = Db::name('mendian_moneylog')
                ->alias('mml')
                ->field('mml.*,mendian.name')
                ->join('mendian mendian','mendian.id=mml.mdid')
                ->where($where)
                ->page($page,$limit)
                ->order($order)
                ->select()
                ->toArray();
            if($data){
                foreach($data as &$v){
                    $v['mendian_infor'] = '';
                    $mendian = Db::name('mendian')->where('id',$v['mdid'])->field('id,name')->find();
                    if($mendian){
                        $v['mendian_infor'] = 'ID:'.$v['mdid']."\n\r"."名称:".$mendian['name'];
                    }
                }
                unset($v);
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data]);
        }
        if(!$this->mdid){
            $mendian = Db::name('mendian')
            ->where('aid',aid)
            ->field('id,name')
            ->select()
            ->toArray();
            View::assign('mendian',$mendian);
        }
        View::assign('mdid',$this->mdid?$this->mdid:0);
        return View::fetch();
    }
    //余额明细导出
    public function moneylogexcel(){
        if(input('param.field') && input('param.order')){
            $order = 'mml.'.input('param.field').' '.input('param.order');
        }else{
            $order = 'mml.id desc';
        }

        $where = [];
        if($this->mdid){
            $where[] = ['mml.mdid','=',$this->mdid];
        }else{
            if(input('param.mdid')){
                $where[] = ['mml.mdid','=',input('param.mdid')];
            }
        }
        $where[] = ['mml.aid','=',aid];
        
        if(input('param.name')) $where[] = ['mendian.name','like','%'.trim(input('param.name')).'%'];
        if(input('param.mid')) $where[] = ['mml.mid','=',trim(input('param.mid'))];

        $list = Db::name('mendian_moneylog')
            ->alias('mml')
            ->field('mendian.name,mml.*')
            ->join('mendian mendian','mendian.id=mml.mdid')
            ->where($where)->order($order)->select()->toArray();
        $title = array();
        $title[] = '门店信息';
        $title[] = t('会员').'信息';
        $title[] = '变更金额';
        $title[] = '变更后剩余';
        $title[] = '变更时间';
        $title[] = '备注';
        $data = array();
        foreach($list as $v){
            $tdata = array();
            $mendian_infor = '';
            $mendian = Db::name('mendian')->where('id',$v['mdid'])->field('id,name')->find();
            if($mendian){
                $mendian_infor = 'ID:'.$v['mdid']."\n\r"."名称:".$mendian['name'];
            }
            $tdata[] = $mendian_infor;
            $tdata[] = $v['nickname'].'('.t('会员').'ID:'.$v['mid'].')';
            $tdata[] = $v['money'];
            $tdata[] = $v['after'];
            $tdata[] = date('Y-m-d H:i:s',$v['createtime']);
            $tdata[] = $v['remark'];
            $data[] = $tdata;
        }
        $this->export_excel($title,$data);
    }
    //余额明细删除
    public function moneylogdel(){
        if($this->mdid){
            return json(['status'=>0,'msg'=>'无操作权限']);
        }
        $ids = input('post.ids/a');

        $where = array();
        $where[] = ['id','in',$ids];
        if($this->mdid){
            $where[] = ['mdid','=',$this->mdid];
        }
        $where[] = ['aid','=',aid];
        Db::name('mendian_moneylog')->where($where)->delete();
        \app\common\System::plog('删除门店余额明细'.implode(',',$ids));
        return json(['status'=>1,'msg'=>'删除成功']);
    }

    //提现记录
    public function withdrawlog(){
        if(request()->isAjax()){
            $page = input('param.page');
            $limit = input('param.limit');
            if(input('param.field') && input('param.order')){
                $order = 'mwl.'.input('param.field').' '.input('param.order');
            }else{
                $order = 'mwl.id desc';
            }
            $where = [];

            if($this->mdid){
                $where[] = ['mwl.mdid','=',$this->mdid];
            }else{
                if(input('param.mdid')){
                    $where[] = ['mwl.mdid','=',input('param.mdid')];
                }
            }

            $where[] = ['mwl.aid','=',aid];
            if(input('param.nickname')) $where[] = ['member.nickname','like','%'.trim(input('param.nickname')).'%'];
            if(input('param.mid')) $where[] = ['mwl.mid','=',trim(input('param.mid'))];
            if(input('?param.status') && input('param.status')!=='') $where[] = ['mwl.status','=',input('param.status')];
            $count = 0 + Db::name('mendian_withdrawlog')->alias('mwl')
                ->field('member.nickname,member.headimg,mwl.*')
                ->join('member member','member.id=mwl.mid')
                ->where($where)
                ->count();
            $data = Db::name('mendian_withdrawlog')
                ->alias('mwl')
                ->field('member.nickname,member.headimg,mwl.*')
                ->join('member member','member.id=mwl.mid')
                ->where($where)->page($page,$limit)->order($order)->select()->toArray();
            if($data){
                foreach($data as &$v){
                    $v['mendian_infor'] = '';
                    $mendian = Db::name('mendian')->where('id',$v['mdid'])->field('id,name')->find();
                    if($mendian){
                        $v['mendian_infor'] = 'ID:'.$mendian['id']."\n\r"."名称:".$mendian['name'];
                    }
                }
                unset($v);
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data]);
        }
        if(!$this->mdid){
            $mendian = Db::name('mendian')
            ->where('aid',aid)
            ->field('id,name')
            ->select()
            ->toArray();
            View::assign('mendian',$mendian);
        }
        View::assign('mdid',$this->mdid?$this->mdid:0);
        return View::fetch();
    }

    //提现记录导出
    public function withdrawlogexcel(){
        if(input('param.field') && input('param.order')){
            $order = 'mendian_withdrawlog.'.input('param.field').' '.input('param.order');
        }else{
            $order = 'mendian_withdrawlog.id desc';
        }
        $where = [];
        if($this->mdid){
            $where[] = ['mendian_withdrawlog.mdid','=',$this->mdid];
        }else{
            if(input('param.mdid')){
                $where[] = ['mendian_withdrawlog.mdid','=',input('param.mdid')];
            }
        }
        $where[] = ['mendian_withdrawlog.aid','=',aid];
        if(input('param.nickname')) $where[] = ['member.nickname','like','%'.trim(input('param.nickname')).'%'];
        if(input('param.mid')) $where[] = ['mendian_withdrawlog.mid','=',trim(input('param.mid'))];
        if(input('?param.status') && input('param.status')!=='') $where[] = ['mendian_withdrawlog.status','=',input('param.status')];
        $list = Db::name('mendian_withdrawlog')->alias('mendian_withdrawlog')->field('member.nickname,member.headimg,mendian_withdrawlog.*')->join('member member','member.id=mendian_withdrawlog.mid')->where($where)->order($order)->select()->toArray();
        $title = array();
        $title[] = '门店信息';
        $title[] = t('会员').'信息';
        $title[] = '提现金额';
        $title[] = '打款金额';
        $title[] = '提现方式';
        $title[] = '收款账号';
        $title[] = '提现时间';
        $title[] = '状态';
        $data = array();
        foreach($list as $v){
            $tdata = array();
             $mendian_infor = '';
            $mendian = Db::name('mendian')->where('id',$v['mdid'])->field('id,name')->find();
            if($mendian){
                $mendian_infor = 'ID:'.$mendian['id']."\n\r"."名称:".$mendian['name'];
            }
            $tdata[] = $mendian_infor;
            $tdata[] = $v['nickname'].'('.t('会员').'ID:'.$v['mid'].')';
            $tdata[] = $v['txmoney'];
            $tdata[] = $v['money'];
            $tdata[] = $v['paytype'];
            if($v['paytype'] == '支付宝'){
                $tdata[] = $v['aliaccountname'].' '.$v['aliaccount'];
            }elseif($v['paytype'] == '银行卡'){
                $tdata[] = $v['bankname'] . ' - ' .$v['bankcarduser']. ' - '.$v['bankcardnum'];
            }else{
                $tdata[] = '';
            }
            $tdata[] = date('Y-m-d H:i:s',$v['createtime']);
            $st = '';
            if($v['status']==0){
                $st = '审核中';
            }elseif($v['status']==1){
                $st = '已审核';
            }elseif($v['status']==2){
                $st = '已驳回';
            }elseif($v['status']==3){
                $st = '已打款';
            }
            $tdata[] = $st;
            $data[] = $tdata;
        }
        $this->export_excel($title,$data);
    }
    //提现记录改状态
    public function withdrawlogsetst(){
        if($this->mdid){
            return json(['status'=>0,'msg'=>'无操作权限']);
        }
        $id = input('post.id/d');
        $st = input('post.st/d');
        $reason = input('post.reason');
        $info = Db::name('mendian_withdrawlog')->where('aid',aid)->where('id',$id)->find();
        if($st==10){//微信打款
            if($info['status']!=1) return json(['status'=>0,'msg'=>'已审核状态才能打款']);
            $rs = \app\common\Wxpay::transfers(aid,$info['mid'],$info['money'],$info['ordernum'],$info['platform'],t('余额').'提现');
            if($rs['status']==0){
                return json(['status'=>0,'msg'=>$rs['msg']]);
            }else{
                Db::name('mendian_withdrawlog')->where('aid',aid)->where('id',$id)->update(['status'=>3,'reason'=>$reason,'paytime'=>time(),'paynum'=>$rs['resp']['payment_no']]);
                //提现成功通知
                $tmplcontent = [];
                $tmplcontent['first'] = '您的提现申请已打款，请留意查收';
                $tmplcontent['remark'] = '请点击查看详情~';
                $tmplcontent['money'] = (string) round($info['money'],2);
                $tmplcontent['timet'] = date('Y-m-d H:i',$info['createtime']);
                \app\common\Wechat::sendtmpl(aid,$info['mid'],'tmpl_tixiansuccess',$tmplcontent,m_url('pages/my/usercenter'));
                //订阅消息
                $tmplcontent = [];
                $tmplcontent['amount1'] = $info['money'];
                $tmplcontent['thing3'] = $info['paytype'];
                $tmplcontent['time5'] = date('Y-m-d H:i');

                $tmplcontentnew = [];
                $tmplcontentnew['amount3'] = $info['money'];
                $tmplcontentnew['phrase9'] = $info['paytype'];
                $tmplcontentnew['date8'] = date('Y-m-d H:i');
                \app\common\Wechat::sendwxtmpl(aid,$info['mid'],'tmpl_tixiansuccess',$tmplcontentnew,'pages/my/usercenter',$tmplcontent);
                //短信通知
                $member = Db::name('member')->where('id',$info['mid'])->find();
                if($member['tel']){
                    $tel = $member['tel'];
                    \app\common\Sms::send(aid,$tel,'tmpl_tixiansuccess',['money'=>$info['money']]);
                }
                \app\common\System::plog('余额提现微信打款'.$id);
                return json(['status'=>1,'msg'=>$rs['msg']]);
            }
        }else{
            Db::name('mendian_withdrawlog')->where('aid',aid)->where('id',$id)->update(['status'=>$st,'reason'=>$reason]);
            if($st == 2){
                //驳回返还余额
                \app\custom\Xixie::addmoney(aid,$info['mdid'],$info['txmoney'],'余额提现返还');

                //提现失败通知
                $tmplcontent = [];
                $tmplcontent['first'] = '您的提现申请被商家驳回，可与商家协商沟通。';
                $tmplcontent['remark'] = $reason.'，请点击查看详情~';
                $tmplcontent['money'] = (string) round($info['txmoney'],2);
                $tmplcontent['time'] = date('Y-m-d H:i',$info['createtime']);
                \app\common\Wechat::sendtmpl(aid,$info['mid'],'tmpl_tixianerror',$tmplcontent,m_url('pages/my/usercenter'));
                //订阅消息
                $tmplcontent = [];
                $tmplcontent['amount1'] = $info['txmoney'];
                $tmplcontent['time3'] = date('Y-m-d H:i',$info['createtime']);
                $tmplcontent['thing4'] = $reason;

                $tmplcontentnew = [];
                $tmplcontentnew['thing1'] = '提现失败';
                $tmplcontentnew['amount2'] = $info['txmoney'];
                $tmplcontentnew['date4'] = date('Y-m-d H:i',$info['createtime']);
                $tmplcontentnew['thing12'] = $reason;
                \app\common\Wechat::sendwxtmpl(aid,$info['mid'],'tmpl_tixianerror',$tmplcontentnew,'pages/my/usercenter',$tmplcontent);
                //短信通知
                $member = Db::name('member')->where('id',$info['mid'])->find();
                if($member['tel']){
                    $tel = $member['tel'];
                    \app\common\Sms::send(aid,$tel,'tmpl_tixianerror',['reason'=>$reason]);
                }
                \app\common\System::plog('余额提现驳回'.$id);
            }
            if($st==3){
                //提现成功通知
                $tmplcontent = [];
                $tmplcontent['first'] = '您的提现申请已打款，请留意查收';
                $tmplcontent['remark'] = '请点击查看详情~';
                $tmplcontent['money'] = (string) round($info['money'],2);
                $tmplcontent['timet'] = date('Y-m-d H:i',$info['createtime']);
                \app\common\Wechat::sendtmpl(aid,$info['mid'],'tmpl_tixiansuccess',$tmplcontent,m_url('pages/my/usercenter'));
                //订阅消息
                $tmplcontent = [];
                $tmplcontent['amount1'] = $info['money'];
                $tmplcontent['thing3'] = $info['paytype'];
                $tmplcontent['time5'] = date('Y-m-d H:i');

                $tmplcontentnew = [];
                $tmplcontentnew['amount3'] = $info['money'];
                $tmplcontentnew['phrase9'] = $info['paytype'];
                $tmplcontentnew['date8'] = date('Y-m-d H:i');
                \app\common\Wechat::sendwxtmpl(aid,$info['mid'],'tmpl_tixiansuccess',$tmplcontentnew,'pages/my/usercenter',$tmplcontent);
                //短信通知
                $member = Db::name('member')->where('id',$info['mid'])->find();
                if($member['tel']){
                    $tel = $member['tel'];
                    \app\common\Sms::send(aid,$tel,'tmpl_tixiansuccess',['money'=>$info['money']]);
                }
                \app\common\System::plog('余额提现改为已打款'.$id);
            }
        }
        return json(['status'=>1,'msg'=>'操作成功']);
    }
    //提现记录删除
    public function withdrawlogdel(){
        if($this->mdid){
            return json(['status'=>0,'msg'=>'无操作权限']);
        }
        $ids = input('post.ids/a');
        $where = array();
        $where[] = ['id','in',$ids];
        if($this->mdid){
            $where[] = ['mdid','=',$this->mdid];
        }
        $where[] = ['aid','=',aid];
        Db::name('mendian_withdrawlog')->where($where)->delete();
        \app\common\System::plog('余额提现记录删除'.implode(',',$ids));
        return json(['status'=>1,'msg'=>'删除成功']);
    }
}
